import request from "@/api";
import { BaseRet, ListRet, PageQuery, PageRet } from "../../response";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";

class MenuApi {
  /**
   * 查询
   * @param data
   * @returns
   */
  static async query(data: MenuQuery): Promise<PageRet<SysMenu>> {
    return await request({
      url: "/sysMenu/index",
      method: "post",
      data: data,
    });
  }
  /**
   * 查询子菜单数据
   * @param data
   * @returns
   */
  static async querySubMenu(id: string): Promise<SysMenu[]> {
    return await request({
      url: `sysMenu/getSubMenu/${id}`,
      method: "get",
    });
  }
  /**
   * 查询父级菜单数据
   * @param data
   * @returns
   */
  static async queryParentMenuTree(id: string): Promise<ListRet<MenuTree>> {
    return await request({
      url: `sysMenu/getParentMenuTree/${id}`,
      method: "get",
    });
  }
  /**
   * 新增
   * @param data
   * @returns
   */
  static async create(data: SysMenu): Promise<BaseRet> {
    return await request({
      url: "/sysMenu/create",
      method: "post",
      data: data,
    });
  }

  /**
   * 更新
   * @param data
   * @returns
   */
  static async update(data: SysMenu): Promise<BaseRet> {
    return await request({
      url: "/sysMenu/update",
      method: "put",
      data: data,
    });
  }

  /**
   * 删除
   * @param id
   * @returns
   */
  static async delete(id: string): Promise<BaseRet> {
    return await request({
      url: `/sysMenu/delete/${id}`,
      method: "delete",
    });
  }

  /**
   * 查询用户菜单数据
   * @param id
   * @returns
   */
  static async queryHomeMenu(): Promise<ListRet<HomeMenuTree>> {
    return await request({
      url: `/sysMenu/getHomeMenuList`,
      method: "get",
    });
  }

  /**
   * 查询用户按钮数据
   * @param id
   * @returns
   */
  static async queryButton(menuId: string): Promise<ListRet<Button>> {
    return await request({
      url: `/sysMenu/getButtonList/${menuId}`,
      method: "get",
    });
  }
}

export default MenuApi;

export interface SysMenu {
  id?: string;
  parentId?: string;
  name?: string;
  icon?: string;
  hasChildren?: boolean;
  menuType?: number;
  apiUrl?: string;
  permission?: string;
  routerName?: string;
  routerUrl?: string;
  webFilePath?: string;
  order?: number;
  hidden: boolean;
  children?: SysMenu[];
}

export interface MenuQuery extends PageQuery {
  searchString: string;
}

export interface MenuTree {
  id?: string;
  parentId?: string;
  name?: string;
  menuType?: number;
  children?: MenuTree[];
}

export interface HomeMenuTree {
  id?: string;
  parentId?: string;
  name?: string;
  icon?: string;
  apiUrl?: string;
  permission?: string;
  routerName?: string;
  routerUrl: string;
  redirect?: string;
  webFilePath?: string;
  menuType?: MenuTypeEnum;
  hidden: boolean;
  children?: HomeMenuTree[];
}

export interface Button {
  id?: string;
  name?: string;
  icon?: string;
  permission?: string;
  apiUrl?: string;
}
